using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class RepeaterPage : System.Web.UI.Page
{
    int pageSize = 10;
    ControlHelper controlHelper;
    protected void Page_Load(object sender, EventArgs e)
    {
        string connectionString =
                WebConfigurationManager.ConnectionStrings[
                "RCVDB2000"].ConnectionString;
        controlHelper = new ControlHelper(connectionString);
        controlHelper.CommandText=
        "select Email,FirstName+ ' '" +
        "+LastName as FullName, Address" +
        ",City from tblJSAccounts";
        controlHelper.CommandType= CommandType.Text;
        controlHelper.InitProperties();
        if (!IsPostBack)
        {
            lblPage.Text = "1";

           
            controlHelper.FillDataTableToRepeater(this.Repeater1,0,
            pageSize);
            controlHelper.CommandText =
            "select count(*) from tblJSAccounts";
            controlHelper.CommandType = CommandType.Text;
            controlHelper.InitProperties();
            int rows = controlHelper.GetTotalRecords();
            lblPages.Text = 
            (rows / pageSize + 
            ((rows % pageSize) > 0 ? 1 : 0)).ToString();   
            if(  rows<= pageSize)
            {
                linkPrevious.Visible=false;
                linkFirst.Visible=false;
                linkLast.Visible=false;
                linkNext.Visible=false;
            }     
            else
            {              
                linkPrevious.Enabled = false;
                linkFirst.Enabled = false;
            }
        }
    }


    protected void linkFirst_Click(object sender, EventArgs e)
    {

        controlHelper.FillDataTableToRepeater(
        this.Repeater1, 0, pageSize);
        lblPage.Text = "1";

        linkPrevious.Enabled = false;
        linkFirst.Enabled = false;
        linkNext.Enabled = true;
        linkLast.Enabled = true;
    }
    protected void linkPrevious_Click(object sender, EventArgs e)
    {
        linkNext.Enabled = true;
        linkLast.Enabled = true;
        int currentPage = Convert.ToInt32(lblPage.Text);
        if (currentPage > 1) currentPage--;
        controlHelper.FillDataTableToRepeater(
        this.Repeater1,
        (currentPage - 1) * pageSize, pageSize);
        lblPage.Text = currentPage.ToString();

        if (currentPage == 1)
        {
            linkPrevious.Enabled = false;
            linkFirst.Enabled = false;
        }
        else
        {
            linkFirst.Enabled = true;
            linkPrevious.Enabled = true;
        }
    }

    protected void linkNext_Click(object sender, EventArgs e)
    {
        linkFirst.Enabled = true;
        linkPrevious.Enabled = true;
        int currentPage = Convert.ToInt32(lblPage.Text);
        if (currentPage > 0) currentPage++;
        controlHelper.FillDataTableToRepeater(
            this.Repeater1, 
            (currentPage - 1) * pageSize,
        pageSize);
        lblPage.Text = currentPage.ToString();

        if (currentPage == 
        Convert.ToInt32(lblPages.Text))
        {
            linkLast.Enabled = false;
            linkNext.Enabled = false;
        }
        else
        {
            linkLast.Enabled = true;
            linkNext.Enabled = true;
        }
    }


    protected void linkLast_Click(object sender, EventArgs e)
    {
        linkNext.Enabled = false;
        linkLast.Enabled = false;
        linkFirst.Enabled = true;
        linkPrevious.Enabled = true;
        controlHelper.FillDataTableToRepeater(
        this.Repeater1, 
        (Convert.ToInt32(lblPages.Text) 
        - 1) * pageSize, pageSize);
        lblPage.Text = lblPages.Text;

    }
}
